<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form">
        <fieldset>
            <div class="form-group">
                <label class="col-md-2 control-label">用户名</label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="用户名" type="text" name="username"
                           data-bv-notempty="true"
                           data-bv-notempty-message="用户名不能为空">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label">密码</label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="密码" type="text" name="password"
                           data-bv-notempty="true"
                           data-bv-notempty-message="密码不能为空">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">姓名</label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="姓名" type="text" name="name" id="name"
                           data-bv-notempty="true" data-bv-notempty-message="姓名不能为空">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">手机号</label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="手机号" type="text" maxlength="11" name="phone"
                           data-bv-notempty="true" data-bv-notempty-message="手机号不能为空">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">邮箱</label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="邮箱" type="text" maxlength="50" name="email">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">性别</label>
                <div class="col-md-10" style="width: 100px">
                    <select class="form-control input-sm" name="sex" id="sex"
                            data-bv-notempty="true" data-bv-notempty-message="性别不能为空">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">证件类型</label>
                <div class="col-md-10" style="width: 200px">
                    <select class="form-control input-sm" name="cerType" id="cerType"
                            data-bv-notempty="true" data-bv-notempty-message="证件类型不能为空"></select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">证件号码</label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="证件号码" type="text" name="cerNo" id="cerNo"
                           data-bv-notempty="true" data-bv-notempty-message="证件类型不能为空"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">角色</label>
                <div class="checkbox" id="roles"></div>
            </div>

            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" onclick="location.href='userList.html'">返回</button>
                        <button class="btn btn-primary" type="submit" onclick="add()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>

        </fieldset>
    </form>
</div>
<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/my/roles/role.js?v=1.0.1"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/dict.js"></script>
<script type="text/javascript">
    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
    });

    showDictSelect("sex", "sex");
    showDictSelect("cerType", "cerType");

    initRoles();

    $('#form').bootstrapValidator();

    function add() {
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }

        var formdata = $("#form").serializeObject();
        formdata.roleIds = getCheckedRoleIds();

        $.ajax({
            type: 'post',
            url: '/users',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            success: function (data) {
                layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                    location.href = "userList.html";
                });
            }
        });
    }

</script>
</body>
</html>